define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'sms_code', title: __('Sms_code'), operate: 'LIKE'},
                        {field: 'bank_card', title: __('Bank_card'), operate: 'LIKE'},
                        {field: 'card_name', title: __('Name'), operate: 'LIKE'},
                        {field: 'order_number', title: __('Order_number'), operate: 'LIKE'},
                        {field: 'goods_names', title: __('已购商品名'), operate: 'LIKE'},
                        // {field: 'goods_info', title: __('商品信息'), operate: 'LIKE', table: table, formatter: function (value, row, index) {
                        //     try {
                        //         const data = JSON.parse(value);
                        //         let displayText = data.map(item => `${item.name} x${item.quantity}`).join(', ');
                        //         return `<span class='clickable' data-details='${encodeURIComponent(value)}'>${displayText}</span>`;
                        //     } catch (e) {
                        //         return value;
                        //     }
                        // }, events: {
                        //     'click .clickable': function (e, value, row, index) {
                        //         try {
                        //             const data = JSON.parse(decodeURIComponent($(this).data('details')));
                        //             let html = '<ul>';
                        //             data.forEach(item => {
                        //                 html += `<li>${item.name} - 价格: ${item.price} - 数量: ${item.quantity}</li>`;
                        //             });
                        //             html += '</ul>';
                        //             Layer.open({ type: 1, content: html, area: ['300px', '300px'] });
                        //         } catch (e) {
                        //             Layer.msg('解析数据失败');
                        //         }
                        //     }
                        // }},
                        {field: 'is_verified', title: __('是否核销'), searchList: {"yes":__('Yes'),"no":__('No')}, formatter: Table.api.formatter.normal},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
